#include <iostream>
using namespace std;

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string>
#include <cstring>

void writeFile(string &content)
{
    const char * filename = "./test.txt";
    int fd = open(filename,O_WRONLY | O_CREAT,0644);
    // int fd1 = open(filename,O_RDWR | O_CREAT,0644);
    if(fd == -1)
    {
        perror("open error:");
    }

    lseek(fd,0,SEEK_END);
    size_t writeBytes = write(fd,(const void*)content.c_str(),content.size());
    close(fd);
}

void writeFile(string &content,int flag)
{
    const char * filename = "./test.txt";
    int fd = open(filename,O_WRONLY | O_CREAT |O_APPEND,0644);
    // int fd1 = open(filename,O_RDWR | O_CREAT,0644);
    if(fd == -1)
    {
        perror("open error:");
    }

    // lseek(fd,0,SEEK_END);
    size_t writeBytes = write(fd,(const void*)content.c_str(),content.size());
    close(fd);
}

int main()
{
    string content;
    while(1)
    {
        cout<<"请输入文件内容：（输入break结束）";
        cin>>content;

        if(content == "break")
        {
            break;
        }

        writeFile(content);
    }
}